//
//  ScopeConverHistoryViewController.m
//  JxCarios
//
//  Created by xcq on 15/3/14.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ScopeConverHistoryViewController.h"
#import "CarAroundDataSource.h"
#import "ScopeConverTableViewCell.h"

static NSString *scoreCell = @"scopeConvert";
@interface ScopeConverHistoryViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UIColor *color;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (retain , nonatomic)NSMutableArray *tableDataSouce;
@property (assign , nonatomic) int currenPage;
@property (retain , nonatomic) NSString *totalPage;
@property (retain , nonatomic) CarAroundDataSource *carAround;
@property (retain , nonatomic) NSDictionary *dic;
@end

/*
 *	用户v币类型1--现金充值
 2--任务
 3--充值油卡
 4--购物抵扣
 5--退款
 */


@implementation ScopeConverHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _currenPage = 1;
    _dic = @{@"1":@"现金充值",@"2":@"任务",@"3":@"充值油卡",@"4":@"购物抵扣",@"5":@"退款"};
    _totalPage = 0;
    [self initAbout];
    [self netRequest];
}

- (void)netRequest {
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    _carAround.page = INTCONVERSTR(_currenPage);
    [_carAround getVbHistory:^(id result) {
        _totalPage = [result customObjectForkey:@"totalPages"];
        _tableDataSouce = [NSMutableArray arrayWithArray: [result customObjectForkey:@"array"]];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView reloadData];
    } anfFail:^(id result) {
        
    }];
}

- (void)initAbout {
    self.title = @"积分兑换纪录";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView registerNib:[UINib nibWithNibName:@"ScopeConverTableViewCell" bundle:nil] forCellReuseIdentifier:scoreCell];
}


- (void)footerRefresh {
    if ([_totalPage integerValue] <= _currenPage) {
        [self.view.window makeToast:@"没有更多数据了哦"];
        [self.tableView footerEndRefreshing];
        return;
    }
    _currenPage ++;
    
     self.carAround.page = [NSString stringWithFormat:@"%d",_currenPage];
    [self.carAround getVbHistory:^(id result) {
        [_tableDataSouce addObjectsFromArray:[result objectForKey:@"array"]];
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];
        
    }anfFail:^(id result) {
        _currenPage --;
        [self.tableView footerEndRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableDataSouce.count == 0 ? 0 : _tableDataSouce.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ScopeConverTableViewCell *scope = [tableView dequeueReusableCellWithIdentifier:scoreCell forIndexPath:indexPath];
    
         scope.vBLabel.text = @"";
        scope.vBLabel.textColor = BUTTUN_COLOR;
     if ([[[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"VB"] hasPrefix:@"-"] ) {
        scope.vBLabel.textColor = [UIColor redColor];
        scope.vBLabel.text = [[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"VB"];
    }else {
        scope.vBLabel.text = [@"+" stringByAppendingString:[[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"VB"]] ;
    }
    
     scope.date.text = [[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"];
    scope.detail.text = [[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"DEPICT"];
    if ([[_dic customObjectForkey:[[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"VB_TYPE"]] isEqualToString:@""]) {
        scope.type.text = @"";
    }else {
        scope.type.text = [@"积分类型:" stringByAppendingString:[_dic customObjectForkey:[[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"VB_TYPE"]]];
    }
    return scope;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}


@end
